package com.itheima.web.request;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/rs")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取username
        String username = request.getParameter("username");
        System.out.println("get解决乱码前 : " + username);

        //解决乱码:GET .getQueryString()
        //乱码原因: tomcat进行解码默认的字符集是ISO-8859-1
        // 先对乱码进行编码转为字节数组
        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
        // 字节数组解码
        username = new String(bytes, StandardCharsets.UTF_8);
        System.out.println("get解决乱码后 : " + username);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*String username = request.getParameter("username");
        System.out.println("post解决乱码前 : " + username);*/
        //解决乱码:POST.getReader()
        request.setCharacterEncoding("UTF-8");//设置字符输入流的编码
        //获取username
        String username = request.getParameter("username");
        System.out.println("post解决乱码后 : " + username);

    }
}
